package schedule

import (
	"context"
	"gitee.com/alex_li/amazon-rpt/cmd/rpt/process/common"
	"gitee.com/alex_li/amazon-rpt/internal/db"
	"gitee.com/alex_li/amazon-rpt/internal/watchdog"
	"github.com/lishimeng/app-starter/broker"
	"github.com/lishimeng/go-log"
	"time"
)

func Setup(ctx context.Context) (err error) {

	debug = false
	watchdog.New(ctx, time.Second, func(ctx2 context.Context) {
		e := taskProc(ctx2, func(task db.BatchSchedule) error {
			createTask(task.Handler, task.Params, time.Now())
			return nil
		})
		if e != nil {
			log.Info(e)
		}
	})
	return
}

var debug = false

func createTask(handlerName string, params string, now time.Time) {
	if debug {
		log.Info("%s:%s", handlerName, params)
		return
	}
	broker.Get().Publish(handlerName, common.TaskInfo{
		Handler:   handlerName,
		CreateAt:  now,
		ParamsB64: params,
	})
}
